package com.cskaoyan.controller.wxcontroller;


import com.cskaoyan.bean.bo.WxOrderListBO;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.OrderParam;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.DearlingOrderListVo;
import com.cskaoyan.bean.vo.OrderDetailDearling;
import com.cskaoyan.bean.bo.OrderIdBo;
import com.cskaoyan.bean.pojo.OrderGoods;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.OrderShowListVO;
import com.cskaoyan.bean.wxbo.CancelIdBo;
import com.cskaoyan.bean.wxbo.OrderCommentBo;
import com.cskaoyan.bean.wxbo.OrderCommitBo;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author ：dailin
 * @date ：
 * @description：
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    WxOrderService wxOrderService;


    //用户申请退款
    @RequestMapping("refund")
    public BaseRespVo refundMoney(@RequestBody OrderIdBo orderId) {

        System.out.println(orderId.getOrderId());
        //根据orderid
        boolean b = orderService.refundMoney(orderId.getOrderId());

        if (b) return BaseRespVo.ok("成功");
        return BaseRespVo.fail("失败");
    }

    //用户收货
    @RequestMapping("confirm")
    public BaseRespVo comfirmOrder(@RequestBody OrderIdBo orderId) {


        boolean b = orderService.confirmMoney(orderId.getOrderId());
        if (b) return BaseRespVo.ok("成功");
        return BaseRespVo.fail("失败");
    }


    //评价页面
    @RequestMapping("goods")
    public BaseRespVo orderGoods(Integer orderId, Integer goodsId) {

        OrderGoods orderGoods =
                orderService.selectOrderGoodsByOrderIdAndGoodsId(orderId, goodsId);

        if (orderGoods != null) return BaseRespVo.ok(orderGoods, "成功");

        return BaseRespVo.fail("失败");
    }



    @RequestMapping("detail")
    public BaseRespVo showOrderDetail(OrderParam orderParam){
        OrderDetailDearling orderDetailVo = orderService.selectOrderDetail(orderParam);
        return BaseRespVo.ok(orderDetailVo);
    }

    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody OrderIdBo orderId) {

        System.out.println(orderId.getOrderId());
        boolean b = orderService.deleteOrderById(orderId.getOrderId());
        if (b) return BaseRespVo.ok("成功");
        return BaseRespVo.fail("失败");
    }

    @RequestMapping("comment")
    public BaseRespVo orderComment(@RequestBody OrderCommentBo orderCommentBo){

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        boolean b = orderService.addComment(orderCommentBo,user);

        if(b)return BaseRespVo.ok("成功");

        return BaseRespVo.fail("失败");

    }


    @RequestMapping("cancel")
    public BaseRespVo cancelOrder(@RequestBody CancelIdBo cancelIdBo){

        boolean b = orderService.cancelOrder(cancelIdBo.getOrderId());

        if(b) return BaseRespVo.ok("取消成功");
        return BaseRespVo.fail("取消失败");
    }

    //提交订单
    @RequestMapping("submit")
    public BaseRespVo submitOrder(@RequestBody OrderCommitBo orderCommitBo){

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();


        boolean b = orderService.submitOrder(orderCommitBo,user);
        if(b) return BaseRespVo.ok("下单成功");
        return BaseRespVo.fail("下单失败");
    }

    //付款
    @RequestMapping("prepay")
    public BaseRespVo prepayOrder(@RequestBody CancelIdBo cancelIdBo){



        boolean b = orderService.prepayOrder(cancelIdBo.getOrderId());

        if(b) return BaseRespVo.ok("付款成功");
        return BaseRespVo.fail("付款失败");

    }


    /**
     *
     * List By Wh
     */
    @RequestMapping("list")
    public BaseRespVo showOrderList(WxOrderListBO wxOrderListBO){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        OrderShowListVO orderShowListVO = wxOrderService.showOrderList(wxOrderListBO,user);
        return BaseRespVo.ok(orderShowListVO);
    }


    /**
     * ListBy Dl
     */

//    @RequestMapping("list")
//    public BaseRespVo selectOrderList(OrderParam orderParam){
//        Subject subject = SecurityUtils.getSubject();
//        User user = (User) subject.getPrincipal();
//        DearlingOrderListVo dearlingOrderListVo = orderService.selectOrderListDl(orderParam,user);
//        return BaseRespVo.ok(dearlingOrderListVo);
//    }




//    @RequestMapping("list")
//    public BaseRespVo selectOrderList(OrderParam orderParam) {
//        BaseData baseData = orderService.selectOrderList(orderParam);
//        return BaseRespVo.ok(baseData);
//    }

}

